package com.healthcloud.controller;

import com.google.common.collect.Lists;
import com.healthcloud.utils.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.Department;
import com.healthcloud.entity.Hospital;
import com.healthcloud.entity.HospitalDepartment;
import com.healthcloud.service.DepartmentService;
import com.healthcloud.service.HospitalService;
import com.healthcloud.utils.ApiRes;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 医院数据rest服务控制器
 *
 * Created by houxianghua on 2015/7/17.
 */
@ApiIgnore
@Api(value = "示例说明", description = "医院数据rest服务控制器")
@RestController
@RequestMapping("/app/hosp")
public class HospitalApiController {
	@Autowired
	private HospitalService hospitalService;

	@Autowired
	private DepartmentService departmentService;

	/**
	 * 根据地区code查询医院列表
	 * @param request
	 * @return JsonMsg
	 */
	@ApiOperation(value = "根据地区code查询医院列表",notes ="根据地区code查询医院列表")
	@RequestMapping(value = "/getHospitalListByAreaCode/get", method = RequestMethod.POST)
	public Object getHospitalListByAreaCodeGet(HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();
		String code = params.get("code")[0].toString();

		List<Hospital> dataList = hospitalService.getHospitalListByAreaCode(code, null);

		for (int i=0;i<dataList.size();i++){
			dataList.get(i).setCreatedTime(null);
			dataList.get(i).setUpdatedTime(null);
		}

		return new ApiRes(dataList);
	}

	/**
	 * 获取科室模版列表
	 * @param depKind 医院类型 1：社区服务站 2：医院
	 * */
	@ApiOperation(value = "获取科室模版列表",notes ="获取科室模版列表")
	@RequestMapping(value = "/getDepartment", method = RequestMethod.POST)
	public Object getDepartment(Integer depKind){
		Department department = new Department();
		department.setIsDel(false);
		department.setDepKind(depKind);
		List<Department> departmentList = departmentService.findByEntityAll(department);
		return new ApiRes(true,departmentList,null,null);
	}

	/**
	 * 根据医院id查询 医院与科室关联信息列表（所含科室，科室别名。。。）
	 * @param hospId 医院id
	 * */
	@ApiOperation(value = "查询 医院与科室关联信息列表",notes ="查询 医院与科室关联信息列表")
	@RequestMapping(value = "/getHospitalDepartmentByHospId", method = RequestMethod.POST)
	public Object getHospitalDepartmentByHospId(Integer hospId){
		//科室信息
		List<HospitalDepartment> hdList = hospitalService.getHospitalDepartmentByHospId(hospId);
		return new ApiRes(true,hdList,null,null);
	}

	@ApiOperation(value = "根据code获取医院列表",notes ="根据code获取医院列表")
	@RequestMapping(value = "/getHospitalList", method = RequestMethod.POST)
	public @ResponseBody List<Hospital> getHospitalList(String areaCode){
		List<Hospital> list = hospitalService.getHospitalListByAreaCode(areaCode, null);
		return list;
	}

	@ApiOperation(value = "获取医院列表",notes ="获取医院列表")
	@RequestMapping(value = "/search")
	public @ResponseBody
    ApiRes searchHospital(Hospital hospital, @ModelAttribute("pageParam")PageParam pageParam){
		List<Hospital> hospitals = hospitalService.searchAll(hospital, pageParam);
		return new ApiRes(true,hospitals,null,null);
	}

	@ApiIgnore
	@ApiOperation(value = "获取指定医院列表 10000036,6876,10000059,10000062",notes ="获取指定医院列表 10000036,6876,10000059,10000062")
	@GetMapping(value = "/listByIds")
	public @ResponseBody
	ApiRes<Hospital> listByIds(){
		List<Hospital> hospitals = hospitalService.listByIds(Lists.newArrayList(10000036L,6876L,10000059L,10000062L));
		return new ApiRes(true,hospitals,null,null);
	}
	
}
